﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Microsoft.Drawing;
using System.IO;
using System.Runtime.InteropServices;

namespace WiMo.Games.Premium.GDIDisplay.ImageAPI
{
    internal class ImagingStream : IStream, IDisposable
    {
        private Stream _stream;

        public ImagingStream(Stream stream)
        {
            _stream = stream;
        }

        public ImagingStream(string fileName)
        {
            _stream = File.Open(fileName, FileMode.Open);
        }

        public void Read(byte[] pv, int cb, IntPtr pcbRead)
        {
            var value = _stream.Read(pv, 0, cb);
            if(pcbRead != IntPtr.Zero)
                Marshal.WriteInt32(pcbRead, value);
        }

        public void Write(byte[] pv, int cb, IntPtr pcbWritten)
        {
            _stream.Write(pv, 0, cb);
            if(pcbWritten != IntPtr.Zero)
                Marshal.WriteInt32(pcbWritten, cb);
        }

        public void Seek(long offset, int origin, IntPtr newPosition)
        {
            var value = _stream.Seek(offset, (SeekOrigin)origin);
            if(newPosition != IntPtr.Zero)
                Marshal.WriteInt64(newPosition, value);
        }

        public void SetSize(long libNewSize)
        {
            throw new NotImplementedException();
        }

        public void CopyTo(IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten)
        {
            throw new NotImplementedException();
        }

        public void Commit(int grfCommitFlags)
        {
            throw new NotImplementedException();
        }

        public void Revert()
        {
            throw new NotImplementedException();
        }

        public void LockRegion(long libOffset, long cb, int dwLockType)
        {
            throw new NotImplementedException();
        }

        public void UnlockRegion(long libOffset, long cb, int dwLockType)
        {
            throw new NotImplementedException();
        }

        public void Stat(out STATSTG pstatstg, int grfStatFlag)
        {
            throw new NotImplementedException();
        }

        public void Clone(out IStream ppstm)
        {
            throw new NotImplementedException();
        }

        public void Dispose()
        {
            try
            {
                if (_stream != null)
                    _stream.Close();
            }
            catch { }
        }
    }
}
